package com.it.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.it.biz.CourseBiz;
import com.it.biz.UsercourseBiz;
import com.it.pojo.Courses;
import com.it.pojo.CoursesExample;
import com.it.pojo.UsercourseExample;
import com.it.pojo.UsercourseKey;
import com.jkoss.pojo.AjaxReturn;
import com.jkoss.pojo.UserCourse;
import com.jkoss.tool.Page;

@Controller
public class CourseAction {
	@Resource
	private CourseBiz courseBiz;
	
	@Resource
	private UsercourseBiz usercourseBiz;
	//获取课程列表
	@RequestMapping("getAllCourse.do")
	public String getAllCourse(Model model,Page<Courses> page) {
		if(page==null) {
			page = new Page<Courses>();
		}
		page.setResults(courseBiz.findAll(page));
		model.addAttribute("page",page);
		return "course/listcourse.jsp";
	}
	//添加课程
	@RequestMapping("addCourse.do")
	public String addCourse(Model model,Courses courses) {
		if(courses!=null) {
			courseBiz.insert(courses);
			return getAllCourse(model,null);
		}
		return null;
	}
	//更新课程
	@RequestMapping("updCourse.do")
	public String updCourse(Model model,Courses course) {
		if(course!=null) {
			courseBiz.updateByPrimaryKey(course);
			return getAllCourse(model,null);
		}
		return null;
	}
	//删除课程
	@RequestMapping("delCourse.do")
	public String delCourse(Model model,Integer cid) {
		courseBiz.deleteByPrimaryKey(cid);
		return getAllCourse(model,null);
	}
	//加入课程
	@RequestMapping("joinCourse.do")
	public String joinCourse(Model model,Integer cid,Integer uid) {
		UsercourseKey key = new UsercourseKey();
		key.setCid(cid);
		key.setUserid(uid);
		usercourseBiz.insert(key);
		return getAllCourse(model,null);
	}
	//批量删除
	@RequestMapping("delAll.do")
	public String delAll(Model model,String cids) {
		if(cids!=null) {
			String[] cid = cids.split(",");
			courseBiz.delAll(cid);
		}
		return getAllCourse(model,null);
	}
	
	//获取课程信息
	@RequestMapping("ajOneCou.do")
	@ResponseBody
	public AjaxReturn ajOneCou(Integer cid) {
		AjaxReturn ajaxReturn = new AjaxReturn();
		Courses course = courseBiz.selectByPrimaryKey(cid);
		if(course!=null) {
			ajaxReturn.setHead("ok");
			ajaxReturn.setBody(course);
		}else {
			ajaxReturn.setHead("err");
		}
		return ajaxReturn;
	}
	//我的课程
	@RequestMapping("getMycourse.do")
	@ResponseBody
	public AjaxReturn getMycourse(Integer uid) {
		AjaxReturn item = new AjaxReturn();
		UsercourseExample example = new UsercourseExample();
		example.createCriteria().andUseridEqualTo(uid);
		List<UsercourseKey> ucs = usercourseBiz.selectByExample(example);
		if(ucs.size()>0&&ucs!=null) {
			Integer[] cids =new Integer[ucs.size()];
			for(int i=0;i<ucs.size();i++) {
				cids[i] = ucs.get(i).getCid();
			}
			List<Courses> courses = courseBiz.selectAll(cids);
			item.setHead("ok");
			item.setBody(courses);
		}else {
			item.setHead("err");
		}
		return item;
	}
	//课程列表
	@RequestMapping("ajAllCou.do")
	@ResponseBody
	public List<Courses> ajAllCou() {
		List<Courses> courses = courseBiz.selectByExample(new CoursesExample());
		return courses;
	}
}
